package lmscript.postscript.syntax;

import lmscript.postscript.syntax.stack.OperandStack;

public abstract class PSObject implements Cloneable {
	public static final int LITERAL = 0;
	public static final int EXECUTABLE = 1;
	public static final String LITERAL_STRING = "literal";
	public static final String EXECUTABLE_STRING = "executable";

	private String name;
	private int type;

	PSObject(String name, int type) {
		this.name = name;
		this.type = type;
	}

	public boolean equals(Object obj) {
		return super.equals(obj);
	}

	public int hashCode() {
		return super.hashCode();
	}

	public int getType() {
		return this.type;
	}

	public String getTypeString() {
		if (isLiteral()) {
			return PSObject.LITERAL_STRING;
		}
		return PSObject.EXECUTABLE_STRING;
	}

	public boolean isLiteral() {
		return this.type == PSObject.LITERAL;
	}

	public boolean isExecutable() {
		return this.type == PSObject.EXECUTABLE;
	}

	public String getName() {
		return this.name;
	}

	public String toString() {
		return this.name;
	}

	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean execute(OperandStack os, boolean check) {
		boolean valid = true;
		if (check) {
			valid = check(os);
		}
		if (valid) {
			return this.execute(os);
		}
		return valid;
	}

	protected void throwError(OperandStack os, ErrorOperator errorOp) {
		os.push(errorOp);
	}

	protected abstract boolean check(OperandStack os);

	public abstract boolean execute(OperandStack os);
}
